(function() {
	var appModule = angular.module('tracecashModule');

	appModule.directive('paymentList', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/payment/payment-list.html',
			require : '^ngModel',
			scope : {
				sinister : '=ngModel',
				config : '='
			}
		};
	});

	appModule.directive('paymentSummary', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/payment/payment-summary.html',
			require : '^ngModel',
			controller : 'PaymentSummaryCtrl',
			scope : {
				sinisterId : '=ngModel'
			}
		};
	});

	appModule.controller('PaymentSummaryCtrl', ['$scope', '$rootScope', '$routeParams', '$http', function($scope, $rootScope, $routeParams, $http) {
		loadPaymentSummary = function() {
			$http.get($rootScope.REST_PATH + '/psummary/generate/' + $scope.sinisterId).success(function(data) {
				$scope.paymentSummary = data;
			});
		};
		$rootScope.$on('paymentReloaded', function() {
			loadPaymentSummary();
		});
	} ]);

	appModule.directive('paymentPersonList', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/payment/payment-person-list.html',
			require : '^ngModel',
			controller : 'PaymentPersonListCtrl',
			scope : {
				recipientRelation : '=ngModel',
				sinister : '=',
				config : '='
			}
		};
	});

	appModule.controller('PaymentPersonListCtrl', ['$scope', '$rootScope', '$routeParams', '$http', '$modal', function($scope, $rootScope, $routeParams, $http, $modal) {
		$scope.approve = function(payment) {
			$http.get($rootScope.REST_PATH + '/payment/approve/' + payment.id).success(function(data) {
				$scope.updateStateAndReserve(data);
			});
		};
		$scope.reject = function(payment) {
			$http.get($rootScope.REST_PATH + '/payment/reject/' + payment.id).success(function(data) {
				$scope.updateStateAndReserve(data);
			});
		};
		$scope.add = function() {
			$scope.open({
				"recipientRelation" : $scope.recipientRelation,
				"sinister" : {
					"id" : $scope.sinister.id
				}
			});
		};
		$scope.edit = function(payment) {
			payment.sinister = new Object();
			payment.sinister.id = $scope.sinister.id;
			$scope.open(payment);
		};
		$scope.list = function() {
			if ($scope.recipientRelation.id != null) {
				$http.get($rootScope.REST_PATH + '/payment/findByPerson/' + $scope.recipientRelation.id).success(function(data) {
					$scope.payments = data.results;
					$rootScope.$broadcast('paymentReloaded');
				});
			}
		};
		$scope.updateStateAndReserve = function(data) {
			$scope.message = data;
			$scope.sinister.sinisterInfo = data.payload.sinisterInfo;
			$scope.sinister.state = data.payload.state;
			$rootScope.$broadcast('updateConfig');
			$scope.list();
		};
		$scope.letter = function() {
			var modalInstance = $modal.open({
				templateUrl : 'html/payment/payment-letter.html',
				controller : 'PaymentLetterCtrl',
				resolve : {
					sinisterId : function() {
						return $scope.sinister.id;
					},
					personId : function() {
						return $scope.recipientRelation.recipient.id;
					}
				}
			});
			modalInstance.result.then(function(data) {
				$scope.message = data;
			}, function() {
			});
		};
		$scope.open = function(payment) {
			var modalInstance = $modal.open({
				templateUrl : 'html/payment/payment-detail.html',
				controller : 'PaymentDetailCtrl',
				size : 'lg',
				resolve : {
					sinisterId : function() {
						return $scope.sinister.id;
					},
					payment : function() {
						return payment;
					}
				}
			});
			modalInstance.result.then(function(data) {
				$scope.updateStateAndReserve(data);
			}, function() {
			});
		};
		$scope.list();
	}]);

	appModule.controller('PaymentLetterCtrl', ['$scope', '$rootScope', '$http', '$modalInstance', '$filter', 'sinisterId', 'personId', function($scope, $rootScope, $http, $modalInstance, $filter, sinisterId, personId) {
		$http.get($rootScope.REST_PATH + '/payment/listPaymentDates/' + sinisterId + '/' + personId).success(function(dates) {
			$scope.paymentDates = dates;
			if ($scope.paymentDates.length > 0) {
				$scope.selectedDate = $scope.paymentDates[0];
			}
		});
		$scope.save = function() {
			$http.get($rootScope.REST_PATH + '/letter/generatePayment/' + sinisterId + '/' + personId + '/' + $scope.selectedDate).success(function(data) {
				if (data.code == $rootScope.CODE_SUCCESS) {
					$modalInstance.close(data);
				} else {
					$scope.message = data;
				}
			});
		};
		$scope.cancel = function() {
			$modalInstance.dismiss('cancel');
		};
	}]);

	appModule.controller('PaymentDetailCtrl', ['$scope', '$rootScope', '$http', '$modalInstance', 'sinisterId', 'payment', function($scope, $rootScope, $http, $modalInstance, sinisterId, payment) {
		$scope.payment = payment;
		$scope.isDisabled = payment.state != null && payment.state != 'PENDING';
		$scope.save = function() {
			$http.post($rootScope.REST_PATH + '/payment/save', $scope.payment).success(function(data) {
				if (data.code == $rootScope.CODE_SUCCESS) {
					$modalInstance.close(data);
				} else {
					$scope.message = data;
				}
			});
		};
		$scope.cancel = function() {
			$modalInstance.dismiss('cancel');
		};
	}]);

})();